package com.controller;

import com.pojo.Goods;
import com.pojo.Member;
import com.pojo.Provider;
import com.pojo.User;
import com.service.InsertService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("insert")
public class InsertController {
    @Autowired
    private InsertService insertService;
    /*
     * 管理员新增用户
     * 1.加载了增加用户界面
     * 2.新增用户信息
     *
     */
    @RequestMapping("insertUserLoad")
    public ModelAndView insertUser(Integer id){
        return new ModelAndView("insert/insertuser");
    }
    @RequestMapping(value = "insertUser",produces = "text/plain;charset=utf-8")
    @ResponseBody
    public String insertUser(String username,String password,String phone,String  address,Integer gender,Integer role,String  birthday) {
        User user = new User();
        user.setUserName(username);
        user.setUserPassword(password);
        user.setPhone(phone);
        user.setAddress(address);
        user.setGender(gender);
        user.setUserRole(role);
        user.setBirthday(birthday);
        int i = insertService.insertUser(user);
        if (i>0){
            return "新增成功，是否返回?";
        }
        return "新增失败，是否返回?";
    }


    /*
     * 管理员新增供应商
     * 1.加载了添加供应商界面
     * 2.新增供应商信息
     *
     */
    @RequestMapping("insertProviderLoad")
    public ModelAndView insertProviderLoad(){
        return new ModelAndView("insert/insertprovider");
    }
    @RequestMapping(value = "insertProvider",produces = "text/plain;charset=utf-8")
    @ResponseBody
    public String insertProvider(String proName,String proCode,String proContact,String proPhone,String proAddress,String proDesc) {
        Provider provider = new Provider();
        provider.setProName(proName);
        provider.setProCode(proCode);
        provider.setProContact(proContact);
        provider.setProPhone(proPhone);
        provider.setProAddress(proAddress);
        provider.setProDesc(proDesc);
        int i = insertService.insertProvider(provider);
        if (i>0){
            return "新增成功，是否返回?";
        }
        return "新增失败，是否返回?";
    }

    /**
     * 管理员新增商品信息
     * 1.加载新增商品信息界面，并传入供应商信息（需要供应商信息以供用户新增商品时选择）
     * 2.新增商品信息
     **/
    @RequestMapping("insertGoodsLoad")
    public ModelAndView insertGoodsLoad(){
        List<Provider> providerToGoods = insertService.getProviderToGoods();
        ModelAndView mv = new ModelAndView("insert/insertgoods");
        mv.addObject("providerToGood",providerToGoods);
        return mv;
    }
    @RequestMapping(value = "insertGoods",produces = "text/plain;charset=utf-8")
    @ResponseBody
    public String insertGoods(String goodsCode,String goodsName,String goodsUnit, String goodsGenre,Integer goodsBid,Integer goodsPric,Integer  provider){
        Goods goods = new Goods();
        goods.setGoodsCode(goodsCode);
        goods.setGoodsName(goodsName);
        goods.setGoodsUnit(goodsUnit);
        goods.setGoodsGenre(goodsGenre);
        goods.setGoodsBid(goodsBid);
        goods.setGoodsPric(goodsPric);
        goods.setProviderid(provider);
        int i = insertService.insertGoods(goods);
        if (i>0){
            return "新增成功，是否返回?";
        }
        return "新增失败，是否返回?";
    }
    /**
     * 管理员新增会员信息
     * 1.加载新增会员信息界面
     * 2.新增会员信息
     **/
    @RequestMapping("insertMemberLoad")
    public ModelAndView insertMemberLoad(){
        return new ModelAndView("insert/insertmember");
    }
    @RequestMapping(value = "insertMember",produces = "text/plain;charset=utf-8")
    @ResponseBody
    public String insertMember(String name, Double discount, String phone, String address, String gender, String code, String birthday){
        Member member = new Member();
        member.setMemberBirthday(birthday);
        member.setMemberCode(code);
        member.setMemberName(name);
        member.setMenberDiscount(discount);
        member.setMenmberAddress(address);
        member.setMenberGender(gender);
        member.setMenmberPhone(phone);
        int i = insertService.insertMember(member);
        if (i>0){
            return "新增成功，是否返回?";
        }
        return "新增失败，是否返回?";
    }
}
